var $articleContent;
var $search;
var $tableFirstColumn;
var $tableOfEachRow;
var $borrowAndInformationButton;
var $borrowButton;
var $informationButton;
var $isBorrow = false;
var $mask;
var $returnMask;
var $cancelButton;
var $confirmButton;
var $borrowPanel;
var $returnPanel;
var $returnTitle;
var $returnDayInput;
var $informationConfirmbutton;
var $whichButton;

$(function () {
    $articleContent = $(".adminProjectManagement .articleDisplay .content");
    $search = $(".adminProjectManagement #search");
    $tableOfEachRow = $(".adminProjectManagement table tr:not(:first)");
    $tableFirstColumn = $tableOfEachRow.find("td:first");
    $borrowAndInformationButton = $(".adminProjectManagement table button");
    $borrowButton = $(".adminProjectManagement table button:contains('删除')");
    $informationButton = $(".adminProjectManagement table button:contains('22')");
    $mask = $(".adminProjectManagement .mask");
    $returnMask = $(".adminProjectManagement .returnMask");
    $cancelButton = $(".adminProjectManagement .borrow input[type=button]");
    $confirmButton = $(".adminProjectManagement input[type=submit]")
    $borrowPanel = $(".adminProjectManagement .borrow");
    $returnPanel = $(".adminProjectManagement .return");
    $returnTitle = $(".adminProjectManagement .return .title");
    $returnDayInput = $(".adminProjectManagement .borrow .returnDay");
    $informationConfirmbutton = $(".adminProjectManagement button[type='button']");

    setArticleDisplayScroll();

    setDetailInformationScroll();

    setSearchFocusListener();

    addBorrowButton();

    setBorrowButtonListener();

//    setDetailInformationListener();

    setTdHoverListener();
});

function setArticleDisplayScroll() {
    $articleContent.mCustomScrollbar({
        theme: "white",
        scrollButtons: {
            enable: false
        },
        autoHideScrollbar: true,
        scrollInertia: 100,
        horizontalScroll: false,
    });


}

function setDetailInformationScroll() {
    $(".adminProjectManagement .detail .detailForScroll").mCustomScrollbar({
        theme: "white",
        scrollButtons: {
            enable: false
        },
        autoHideScrollbar: true,
        scrollInertia: 100,
        horizontalScroll: false,
    });
}

function setDetailInformationListener() {
    $(".detailInformation").click(function () {
        $(".adminProjectManagement .adimnMask").fadeIn("fast");
        $(".adminProjectManagement .detail").fadeIn("fast");
        $(".adminProjectManagement .hiddens").val($(this).parent().find(".detailInformationForHidden").val());
        alert($(".adminProjectManagement .hiddens").val());
        return false;
    });
    $(".adminProjectManagement .detail .title a").click(function () {
        $(".adminProjectManagement .adimnMask").fadeOut("fast");
        $(".adminProjectManagement .detail").fadeOut("fast");
    })
    $(".adminProjectManagement .adimnMask").click(function () {
        $(".adminProjectManagement .adimnMask").fadeOut("fast");
        $(".adminProjectManagement .detail").fadeOut("fast");
    })
}

function setSearchFocusListener() {
    $search.focus(function () {
        $search.velocity({
            width: "180px"
        }, 200, "ease");
        addSearchListener();
    });
    $search.blur(function () {
        $search.velocity({
            width: "100px"
        }, 200, "ease")
    });
}

function addSearchListener() {
    $search.on('input', function () {
        for (var i = 0; i < $tableFirstColumn.length; i++) {
            var $oneTd = $tableFirstColumn.get(i).innerHTML.toLowerCase();
            while ($oneTd.indexOf(" ") != -1) {
                $oneTd = $oneTd.replace(" ", "");
            }
            if ($oneTd.search($search.val().toLowerCase()) != -1) {
                $tableOfEachRow.get(i).style.display = "block";
            } else {
                $tableOfEachRow.get(i).style.display = "none";
            }
            var $array = $search.val().toLowerCase().split(" ");
            for (var j = 0; j < $array.length; j++) {
                if ($oneTd.search($array[j]) != -1) {
                    if ($array[j] != "") {
                        $tableOfEachRow.get(i).style.display = "block";
                    }
                }
            }
        }
    });
    //	(function(event){//这种做法输入中文的时无反应
    //		setTimeout(function(){
    //			//当用户按下按键的时候，JavaScript 引擎需要执行 keydown 的事件处理程序，
    //			//然后更新文本框的 value 值，这两件事也需要按顺序来，事件处理程序执行时，更
    //			//新 value 值的任务则进入队列等待。所以我们在 keydown 的事件处理程序里是无
    //			//法得到更新后的 value 的，利用 setTimeout，我们把取 value 的操作放入队
    //			//列，放在更新 value 值以后，这样便达到了目的。
    //			$("title").html($search.val());
    //	}, 0);
}
;

function addBorrowButton() {
    $borrowAndInformationButton.hover(function () {
        if ($(this).html() == "删除") {
            $isBorrow = true;
            $(this).html("删除");
        }
    }, function () {
        if ($isBorrow) {
            $(this).html("删除");
        }
    });
//    $(this).html("删除");
}
;

function setBorrowButtonListener() {

    $borrowButton.click(function () {
        $(".borrow form .getID").val($(this).parent().find("input").val());
        $mask.fadeIn("fast")
        $borrowPanel.fadeIn("fast");
        $(".adminProjectManagement .borrow .title").html("删除：" + $(this).parent().parent().find("td:first").html())
//		$returnDayInput.focus();
        $whichButton = $(this);
    });
    $returnDayInput.focus(function () {
        $(".adminProjectManagement .borrow .borrowError").fadeOut("fast");
    });
    $confirmButton.click(function () {
//		var re = /\d{4}-\d{1,2}-\d{1,2}/;
//		var value = $returnDayInput.val();
//		var isValid = re.exec(value);
//		if (isValid != null) {
//			$returnDayInput.val(isValid);
//			return true;
//		}else{
//			$(".adminProjectManagement .borrow .borrowError").fadeIn("fast");
//			return false;
//		}
        return true;
    });

    $cancelButton.click(function () {
        $mask.fadeOut("fast");
        $borrowPanel.fadeOut("fast");
    });
    $mask.click(function () {
        $mask.fadeOut("fast");
        $borrowPanel.fadeOut("fast");
    });

    $informationButton.click(function () {

        linkInformation($(this));

        $returnMask.fadeIn("fast");
        $returnPanel.fadeIn("fast");
        $returnTitle.html($(this).parent().find("td:first").html());
    });
    $informationConfirmbutton.click(function () {
        $returnMask.fadeOut("fast");
        $returnPanel.fadeOut("fast");
    });
    $returnMask.click(function () {
        $returnMask.fadeOut("fast");
        $returnPanel.fadeOut("fast");
    });
    $cancelButton.click(function () {
        $mask.fadeOut("fast");
        $borrowPanel.fadeOut("fast");
    })
}

function linkInformation($clickButton) {
    var $tr = $clickButton.parent().parent();
    var $tableInf = $(".adminProjectManagement .return table");
    $tableInf.find(".preditReturnTime").html($tr.find("td:nth-child(3)").html());
    $tableInf.find(".lendTime").html($tr.find("td:nth-child(4)").html());
    $tableInf.find(".borrower").html($tr.find("td:nth-child(5)").html());
    $tableInf.find(".shortNumber").html($tr.find("td:nth-child(6)").html());
    $tableInf.find(".QQNumber").html($tr.find("td:nth-child(7)").html());
    $tableInf.find(".longNumber").html($tr.find("td:nth-child(8)").html());
    $tableInf.find(".currentAddress").html($tr.find("td:nth-child(9)").html());
}

function setTdHoverListener() {
    $(".adminProjectManagement .content .members").hover(function () {

        $(".adminProjectManagement .overflowTip")
                .html($(this).html());

        if ($(this).html().length > 8) {

            $(".adminProjectManagement .overflowTip").stop(true, true);

            var topDis = Math.round($(this).offset().top);
            var leftDis = Math.round($(this).offset().left);
            var spanWidth = $(".adminProjectManagement .overflowTip").width();
            var tdWidth = $(this).width();
//                        alert(tdWidth);
            var shouldTop = topDis - 45;
            var finalTop = topDis - 35;
            var shouldLeft = leftDis + tdWidth / 2 - spanWidth / 2 - 10;

            $(".adminProjectManagement .overflowTip")
                    .css({
                        left: shouldLeft + "px",
                        top: shouldTop + "px",
                        opacity: "0",
                        display: "block"});
            $(".adminProjectManagement .overflowTip")
                    .animate({
                        opacity: "1",
                        top: finalTop + "px"
                    }, 200);
        }
    }, function () {
        $(".adminProjectManagement .overflowTip").css({display: "none"});
    });
    
    $(".adminProjectManagement .content .projectName").hover(function () {
        $(".adminProjectManagement .overflowTip")
                .html($(this).html());


            $(".adminProjectManagement .overflowTip").stop(true, true);

            var topDis = Math.round($(this).offset().top);
            var leftDis = Math.round($(this).offset().left);
            var spanWidth = $(".adminProjectManagement .overflowTip").width();
            var tdWidth = $(this).width();
//                        alert(tdWidth);
            var shouldTop = topDis - 45;
            var finalTop = topDis - 35;
            var shouldLeft = leftDis + tdWidth / 2 - spanWidth / 2 - 10;

            $(".adminProjectManagement .overflowTip")
                    .css({
                        left: shouldLeft + "px",
                        top: shouldTop + "px",
                        opacity: "0",
                        display: "block"});
            $(".adminProjectManagement .overflowTip")
                    .animate({
                        opacity: "1",
                        top: finalTop + "px"
                    }, 200);
    }, function () {
        $(".adminProjectManagement .overflowTip").css({display: "none"});
    });
}

